<?php

/**
 * @see Zend_Db_Table_Row_Abstract
 */
require_once 'Zend/Db/Table/Row/Abstract.php';

class Tavs_Db_Table_Row extends Zend_Db_Table_Row_Abstract
{
	/**
	 * Mapeamento das colunas
	 *
	 * @var array
	 */
	protected $_columnMap = array();

	/**
	 * Checa se existe uma coluna mapeada em $_columnMap
	 *
	 * @param string $columnName Coluna a ser checada
	 *
	 * @return string Nome da coluna na tabela
	 */
	protected function _transformColumn($columnName)
	{
		$columnName = parent::_transformColumn($columnName);

		if ($key = array_search($columnName, $this->_columnMap)) {
			$columnName = $key;
		}

		return $columnName;
	}

	/**
	 * Limpa um array removendo as colunas inexistentes
	 *
	 * @param array $data Array a ser limpado
	 *
	 * @return array
	 */
	public function clearMappedData(array $data)
	{
		$table = $this->getTable();
		$columns = array_keys($table->info(Zend_Db_Table_Abstract::METADATA));

		$clearData = array();

		foreach ($data as $mappedColumn=>$value) {
			$columnName = $this->_transformColumn($mappedColumn);
			if (in_array($columnName, $columns)) {
				$clearData[$columnName] = $value;
			}
		}

		return $clearData;
	}
}